﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Net;
using System.Runtime.InteropServices;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 标注样式
    /// </summary>
    /// <remarks>对 SketchUpAPI 中的 SUDimensionStyleRef 的包装</remarks>
    public class DimensionStyle : ObjectBase
    {
        #region API 函数
        /// <summary>
        /// 字体
        /// </summary>
        /// <param name="style"></param>
        /// <param name="font"><see cref="Objects.Font"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetFont(IntPtr style, out IntPtr font);

        /// <summary>
        /// 是否有3D文字
        /// </summary>
        /// <param name="style"></param>
        /// <param name="has_3d"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGet3D(IntPtr style, out bool has_3d);

        /// <summary>
        /// 获取箭头类型
        /// </summary>
        /// <param name="style"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetArrowType(IntPtr style, out ArrowType type);

        /// <summary>
        /// 获取箭头尺寸
        /// </summary>
        /// <param name="style"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetArrowSize(IntPtr style, out int size);

        /// <summary>
        /// 获取标注颜色
        /// </summary>
        /// <param name="style"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetColor(IntPtr style, ref Color color);

        /// <summary>
        /// 获取文字颜色
        /// </summary>
        /// <param name="style"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetTextColor(IntPtr style, ref Color color);

        /// <summary>
        /// 获取延长线偏移
        /// </summary>
        /// <param name="style"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetExtensionLineOffset(IntPtr style, out int offset);

        /// <summary>
        /// 延长线超出长度
        /// </summary>
        /// <param name="style"></param>
        /// <param name="overshoot"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetExtensionLineOvershoot(IntPtr style, out int overshoot);

        /// <summary>
        /// 线宽
        /// </summary>
        /// <param name="style"></param>
        /// <param name="weight"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetLineWeight(IntPtr style, out int weight);

        /// <summary>
        /// 是否高亮非关联标注
        /// </summary>
        /// <param name="style"></param>
        /// <param name="highlight"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetHighlightNonAssociativeDimensions(IntPtr style, out bool highlight);

        /// <summary>
        /// 高亮颜色
        /// </summary>
        /// <param name="style"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetHighlightNonAssociativeDimensionsColor(IntPtr style, ref Color color);

        /// <summary>
        /// 是否显示直径标注的前缀
        /// </summary>
        /// <param name="style"></param>
        /// <param name="show"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetShowRadialPrefix(IntPtr style, out bool show);

        /// <summary>
        /// 是否隐藏平面外标注
        /// </summary>
        /// <param name="style"></param>
        /// <param name="hide"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetHideOutOfPlane(IntPtr style, out bool hide);

        /// <summary>
        /// 用于判断是否属于平面外的公差
        /// </summary>
        /// <param name="style"></param>
        /// <param name="tolerance"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetHideOutOfPlaneValue(IntPtr style, out double tolerance);

        /// <summary>
        /// 隐藏太小的标注
        /// </summary>
        /// <param name="style"></param>
        /// <param name="hide"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetHideSmall(IntPtr style, out bool hide);

        /// <summary>
        /// 用于判断是否太小的范围
        /// </summary>
        /// <param name="style"></param>
        /// <param name="tolerance"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionStyleGetHideSmallValue(IntPtr style, out double tolerance);
        #endregion

        /// <summary>
        /// 字体
        /// </summary>
        public Font Font
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetFont(Id, out IntPtr id));
                return new Font(id);
            }
        }

        /// <summary>
        /// 是否有3D文字
        /// </summary>
        public bool Has3DText
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGet3D(Id, out bool h));
                return h;
            }
        }

        /// <summary>
        /// 箭头类型
        /// </summary>
        public ArrowType ArrowType
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetArrowType(Id, out ArrowType at));
                return at;
            }
        }

        /// <summary>
        /// 箭头大小
        /// </summary>
        public int ArrowSize
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetArrowSize(Id, out int s));
                return s;
            }
        }

        /// <summary>
        /// 颜色
        /// </summary>
        public Color Color
        {
            get
            {
                Color c = new Color();
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetColor(Id, ref c));
                return c;
            }
        }

        /// <summary>
        /// 文字颜色
        /// </summary>
        public Color TextColor
        {
            get
            {
                Color c = new Color();
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetTextColor(Id, ref c));
                return c;
            }
        }

        /// <summary>
        /// 延长线偏移
        /// </summary>
        public int ExtensionLineOffset
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetExtensionLineOffset(Id, out int s));
                return s;
            }
        }

        /// <summary>
        /// 延长线长度
        /// </summary>
        public int ExtensionLineOvershoot
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetExtensionLineOvershoot(Id, out int s));
                return s;
            }
        }

        /// <summary>
        /// 线宽
        /// </summary>
        public int LineWeight
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetLineWeight(Id, out int s));
                return s;
            }
        }

        /// <summary>
        /// 是否高亮非关联标注
        /// </summary>
        public bool HighlightNonAssociativeDimension
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetHighlightNonAssociativeDimensions(Id, out bool h));
                return h;
            }
        }

        /// <summary>
        /// 高亮颜色
        /// </summary>
        public Color HighlightColor
        {
            get
            {
                Color c = new Color();
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetHighlightNonAssociativeDimensionsColor(Id, ref c));
                return c;
            }
        }

        /// <summary>
        /// 显示直径标注的前缀
        /// </summary>
        public bool ShowRadialPrefix
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetShowRadialPrefix(Id, out bool h));
                return h;
            }
        }

        /// <summary>
        /// 隐藏平面外标注
        /// </summary>
        public bool HideOutOfPlane
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetHideOutOfPlane(Id, out bool h));
                return h;
            }
        }

        /// <summary>
        /// 用于判断是否超出平面的误差值
        /// </summary>
        public double OutOfPlaneTolerance
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetHideOutOfPlaneValue(Id, out double v));
                return v;
            }
        }

        /// <summary>
        /// 隐藏过小的标注
        /// </summary>
        public bool HideSmall
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetHideSmall(Id, out bool h));
                return h;
            }
        }

        /// <summary>
        /// 用于判断是否太小的误差值
        /// </summary>
        public double SmallTolerance
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionStyleGetHideSmallValue(Id, out double v));
                return v;
            }
        }

        internal DimensionStyle(IntPtr id) => Id = id;
    }
}